package com.xqkad.myplayer.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xqkad.myplayer.R;
import com.xqkad.myplayer.activity.ReceiverActivity;
import com.xqkad.myplayer.info.MusicInfo;
import com.xqkad.myplayer.service.BroadcastService;
import com.xqkad.myplayer.service.MediaService;
import com.xqkad.myplayer.service.MusicPlayer;
import com.xqkad.myplayer.utils.AudioTools;

import java.util.ArrayList;
import java.util.List;

public class MusicFragment extends ReceiverFragment {
    private Activity mContext;
    private RecyclerView recyclerView;
    private Adapter mAdapter;
    private static final String ARG_TITLE="title";
    private String mTitle = "";

    public static MusicFragment newInstance(String title) {
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        MusicFragment fragment = new MusicFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(ARG_TITLE);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = (Activity)context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music,container,false);
        recyclerView = (RecyclerView) view;
        setupRecyclerView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Runnable runn = new Runnable() {
            @Override
            public void run() {
                if(mAdapter!=null){
                    List<MusicInfo> list = AudioTools.getNetMusicInfoList(mTitle);
                    if(list != null)
                        mAdapter.updateDatas(list);
                }
            }
        };
        new Thread(runn).start();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }

    @Override
    public void onReceive(Intent intent) {
        //Log.d("mytag", "musicFRagmentRev: "+intent.getAction());
        String action = intent.getAction();
        if(ON_PLAY.equals(action)){
            int pos = MusicPlayer.getPosition();
            mAdapter.notifyDataSetChanged();
            if(pos != -1){
                recyclerView.smoothScrollToPosition(pos);
            }else
                Log.d("mytag", "onReceive: pos -1");
        }
    }

    private void setupRecyclerView(){
        //Log.d("mytag", "setupRecyclerView: ");
        GridLayoutManager layoutParams = new GridLayoutManager(mContext, 1);
        recyclerView.setLayoutManager(layoutParams);
        mAdapter = new Adapter();
        recyclerView.setAdapter(mAdapter);
    }

    private class Adapter extends RecyclerView.Adapter{
        private List<MusicInfo> mData = new ArrayList<MusicInfo>();

        public void updateDatas(List<MusicInfo> mData) {
            if(mData == null)
                return;
            this.mData = mData;
            mContext.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    notifyDataSetChanged();
                }
            });
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_music,
                    viewGroup, false);
            return new ItemHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
            final View item = ((ItemHolder)viewHolder).getView();
            ImageView image = item.findViewById(R.id.item_music_img);
            TextView text = item.findViewById(R.id.item_music_tv);

            if(MusicPlayer.getMusicId().equals(mData.get(i).id)){
                text.setTextColor(Color.argb(255,255,127,0));
            }else
                text.setTextColor(Color.argb(255,222,222,222));
            image.setImageResource(R.drawable.music);
            text.setText(""+mData.get(i).title);
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MusicPlayer.open(mData,i);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        class ItemHolder extends RecyclerView.ViewHolder{
            private View mView;
            public View getView(){
                return mView;
            }
            public ItemHolder(@NonNull View itemView) {
                super(itemView);
                mView = itemView;
            }
        }
    }
}

